﻿using System;
using System.Web.UI;
using ReadyApps.Common;
using ReadyApps.Data;
using ReadyApps.Wiki.Templates;

namespace ReadyApps.Wiki.Views
{
    class WikiContentRevisionView : WikiContentViewBase
    {
        #region Variables and Constants

        private WikiControlSettings controlSettings;
        private int revisionNumber;

        #endregion

        #region Events

        private event EventHandler viewRevisionsButtonClick;
        public event EventHandler ViewRevisionsButtonClick
        {
            add { viewRevisionsButtonClick += value; }
            remove { viewRevisionsButtonClick -= value; }
        }

        private event EventHandler cloneToHeadButtonClick;
        public event EventHandler CloneToHeadButtonClick
        {
            add { cloneToHeadButtonClick += value; }
            remove { cloneToHeadButtonClick -= value; }
        }

        #endregion

        #region Constructor

        public WikiContentRevisionView(int wikiPageID, int wikiRevisionNumber, IWikiDataProvider wikiDataProvider,
            IWikiUrlManager wikiUrlManager, WikiControlSettings wikiControlSettings)
            : base(wikiPageID, wikiDataProvider, wikiUrlManager)
        {
            if (wikiControlSettings == null)
            {
                throw new ArgumentNullException("wikiControlSettings");
            }
            controlSettings = wikiControlSettings;
            revisionNumber = wikiRevisionNumber;
        }

        #endregion

        #region Public Methods

        public override void BuildControlTree(ControlCollection controls, bool raiseEvents)
        {
            Control container = new Control();

            // Instantiate the template
            ITemplate layoutTemplate = controlSettings.LayoutTemplate ?? new WikiDefaultLayoutTemplate();
            layoutTemplate.InstantiateIn(container);

            // Locate the content and wiki controls control
            Control contentControl = WebUtilities.FindControlRecursive(container, WikiController.CONTENT_CONTROL_ID);
            if (contentControl == null)
            {
                throw new Exception(string.Format("A server control with an ID of '{0}' was not found in the display template.", WikiController.CONTENT_CONTROL_ID));
            }
            Control controlsControl = WebUtilities.FindControlRecursive(container, WikiController.CONTROLS_CONTROL_ID);
            if (controlsControl == null)
            {
                throw new Exception(string.Format("A server control with an ID of '{0}' was not found in the display template.", WikiController.CONTROLS_CONTROL_ID));
            }

            // Fill the page content
            GetContentControls(dataProvider.GetWikiPageDataForRevision(pageID, revisionNumber), contentControl);

            WikiControlButton manageRevisions = WikiControlButton.GetFromTemplate(controlSettings.ButtonTemplate);
            manageRevisions.ButtonControl.Text = "Revisions";
            manageRevisions.ButtonControl.Click += viewRevisionsButtonClick;
            controlsControl.Controls.Add(manageRevisions.RootControl);

            WikiControlButton cloneToHead = WikiControlButton.GetFromTemplate(controlSettings.ButtonTemplate);
            cloneToHead.ButtonControl.Text = "Make Current";
            cloneToHead.ButtonControl.Click += cloneToHeadButtonClick;
            controlsControl.Controls.Add(cloneToHead.RootControl);

            controls.Clear();
            controls.Add(container);
        }

        #endregion
    }
}
